import axios from 'axios';
import Vue from "vue";
import iview from 'iview';
import config from "../../config/index";
const defaults = {
  //baseURL: 'http://127.0.0.1:8081/api',
  baseURL:process.env.NODE_ENV!='server'?'/api':config.hosturl,
  timeout:1000*3,
  //withCredentials:false,
  transformResponse: [(data) => {
  	//console.log(data);
    return JSON.parse(data)
  }]
}

Object.assign(axios.defaults, defaults)
axios.interceptors.request.use(
  config => {
    if(process.env.NODE_ENV!='server'){
      iview.LoadingBar.start();
    }
      
      // if (store.getters.getToken) {
      //     config.headers.Authorization = `Bearer ${store.getters.getToken}`;
      // }
      return config;
  },
  err => {
      return Promise.reject(err);
});
axios.interceptors.response.use(response=>{
  if(process.env.NODE_ENV!='server'){
    iview.LoadingBar.finish();
  }
  if (response.data.errorCode != 0) {
    if (response.data.message) {
        iview.Message.error({
            content: response.data.message,
            duration: 5,
            closable: true
        });

    }
  }
	return response.data;
},error=>{
  if(process.env.NODE_ENV!='server'){
    iview.LoadingBar.error();
  }
  if (error.response) {
    switch (error.response.status) {
        case 500:
            iview.Message.error("服务器异常")
            break;
        case 504:
            iview.Message.error("网络错误")
            break;
        case 403: // 403 清除token信息并跳转到登录页面
            iview.Message.error({
                content: '您没有权限访问该页面！',
                duration: 2
            });
            //router.replace('/');
            break;
        default:
            iview.Message.error('请求错误！');
    }
} else {
    iview.Message.error('网络错误！');
}
	return Promise.reject(error);
});

//export default axios;
Vue.prototype.axios=axios

export var _axios=axios;
export function getlist(){
	//console.log('api getlist')
	return axios.post('/list');
}
export function getuser(){
  return axios.get('/user/user')
}

